package com.wwh.memo

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.wwh.databasetest.MemoDatabaseHelper
import com.wwh.memo.logic.dao.MemoDAO
import com.wwh.memo.logic.model.Database
import com.wwh.memo.logic.model.Memo
import com.wwh.memo.logic.utils.Utils
import com.wwh.memo.ui.MemoAdapter
import com.wwh.memo.ui.MemoContentActivity

class MainActivity : AppCompatActivity() {
//    private val memoDAO = MemoDAO(this, 1)  不可以放在这里！！this还未创建呢！！！！
    lateinit var adapter: MemoAdapter
    lateinit var memoList: ArrayList<Memo>
    lateinit var memoDAO: MemoDAO

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        memoDAO = MemoDAO(this, Database.DATABASE_VERSION)
        memoList = memoDAO.queryMemos() as ArrayList


        setSupportActionBar(findViewById(R.id.toolbar))  // 设置标题栏

        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)  // 瀑布流，两列
//        val layoutManager = GridLayoutManager(this, 2)  // 宫格，两列
        findViewById<RecyclerView>(R.id.recycleView)?.layoutManager = layoutManager
        adapter = MemoAdapter(memoList)
        findViewById<RecyclerView>(R.id.recycleView)?.adapter = adapter

        //下拉刷新（此处只是显示一个提示，并非真的刷新）
        val swipeRefresh = findViewById<SwipeRefreshLayout>(R.id.swipeRefresh)
        swipeRefresh.setColorSchemeResources(com.google.android.material.R.color.design_default_color_primary)
        swipeRefresh.setOnRefreshListener {
            val handler = Handler(Looper.getMainLooper())
            handler.postDelayed({
                Toast.makeText(this, "悟已往之不谏\n知来者之可追", Toast.LENGTH_LONG).show()
                swipeRefresh.isRefreshing = false
            }, 500)
        }

        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener {  // 新建备忘录
            MemoContentActivity.actionStart(this, Memo(-1, "", "", Utils.getDateTime()))  // 自定义，-1为新建的备忘录
        }

    }

    override fun onResume() {
        super.onResume()
        memoList.clear()  // 由于adapter监听的始终是同一个list对象，因此有必要进行更新
        memoList.addAll(memoDAO.queryMemos())
        adapter.notifyDataSetChanged()
        //Log.d("Main", "onResume")
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.backup -> Toast.makeText(this, "备份功能待开发，敬请期待！", Toast.LENGTH_SHORT).show()
            R.id.settings -> Toast.makeText(this, "设置功能待开发，敬请期待！", Toast.LENGTH_SHORT).show()
            R.id.question -> {
//                Toast.makeText(this, "更多功能正在研发，敬请期待！", Toast.LENGTH_SHORT).show()
                val builder: AlertDialog.Builder = AlertDialog.Builder(this)
                builder
                    .setTitle("关于备忘录APP")
                    .setMessage("开发者-Albert\n" +
                            "版本号-1.12\n" +
                            "学校-广东工业大学\n" +
                            "联系我-albertwwh@foxmail.com\n" +
                            "开发完成时间-2024/11/11 11:11\n" +
                            "Gitee仓库-https://gitee.com/albert_musk/Memo")
                    .setPositiveButton("确认") { dialog, which ->
                        Toast.makeText(this, "感谢您的使用！", Toast.LENGTH_SHORT).show()
                    }
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        }

        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        memoDAO.destroy()  // 释放资源
    }
}